package com.zyh;

import com.zyh.util.JedisUtils;
import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.Random;

public class test {
    private static final String PHONE="15012345678";
    private static final String COUNTKEY=PHONE+":count";
    private static final String CODEKEY=PHONE+":code";
    @Test
    public void testSet(){
        Jedis jedis = new Jedis("192.168.193.100",6379);
        jedis.set("name","zhangsan");
        jedis.close();
    }
    @Test
    public void testGet(){
        Jedis jedis = new Jedis("192.168.193.100",6379);
        System.out.println(jedis.get("name"));
        jedis.close();
    }
    @Test
    public void testPoll(){
        //获取连接
        Jedis jedis = JedisUtils.getJedis();
        System.out.println(jedis.get("name"));
        jedis.close();
    }

    @Test
    public void getCode(){
        //获取连接
        Jedis jedis = JedisUtils.getJedis();
        String count = jedis.get(COUNTKEY);
        if(count==null){
            jedis.setex(COUNTKEY,24*60*60,"1");
        }else{
            if(Integer.parseInt(count)<3){
                jedis.incr(COUNTKEY);
            }else{
                System.out.println("超过3次！");
            }
        }
        String code = test.getRandom();
        jedis.setex(CODEKEY,120,code);
        System.out.println("发送了验证码："+code);
        jedis.close();
    }

    @Test
    public void verifyCode(){
        //获取连接
        Jedis jedis = JedisUtils.getJedis();
        String realCode = "730283";
        String code = jedis.get(CODEKEY);
        jedis.close();
        if(realCode.equalsIgnoreCase(code)){
            System.out.println("正确！");
        }else{
            System.out.println("有误！");
        }
    }


    public static String getRandom(){
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        for(int i =0;i<6;i++){
            code.append(random.nextInt(10)+"");
        }
        return code.toString();
    }
}
